
'''group3'''
import re


def group_3(tree):
    dict_group_3 = {"父亲": "", "母亲": "", "兄弟姐妹": "", "子女": "", "血型": "", "RH": "", "药物过敏史": "", "暴露史": "", "遗传病史": "",
                    "残疾情况": "", "其他残疾": ""}
    list_xuexing = ["A型", "B型", "O型", "AB型", "不祥"]
    try:
        xuexing = tree.xpath('//input[@name="EHRBASE.bloodType" and @checked="checked"]/@value')[0]
        dict_group_3["血型"] = list_xuexing[int(xuexing) - 1]
    except:
        dict_group_3["血型"] = None

    list_RH = ["阴性", "阳性", "不祥"]
    try:
        RH = tree.xpath('//input[@name="EHRBASE.rhBloodType" and @checked="checked"]/@value')[0]
        dict_group_3["RH"] = list_RH[int(RH) - 1]
    except:
        dict_group_3["RH"] = None

    try:
        guomin = tree.xpath('//*[@id="ehrForm"]/div[26]/input[1]/@value')[0]
        guomin_you = tree.xpath('//*[@id="ehrForm"]/div[26]/input[2]/@value')[0]
        if guomin == "1":
            dict_group_3["药物过敏史"] = "无"

        if dict_group_3["药物过敏史"] == "无":
            pass
        elif guomin_you != "":
            li_guomin = ["青霉素", "磺胺", "链霉素", "其他"]
            list_guomin_big = []
            selected = tree.xpath('//input[@name="EHRDETAILS.allergyHistory"]')[2:6]
            for i in selected:
                if i.get('checked'):
                    list_guomin_big.append(li_guomin[int(i.get('value')) - 2])
            dict_group_3["药物过敏史"] = "，".join(list_guomin_big)
            if "其他" in list_guomin_big:
                guominkuang = tree.xpath('//input[@name="EHRDETAILS.allergyDescription"]/@value')[0]
                dict_group_3["药物过敏史"] = dict_group_3["药物过敏史"]+"|"+guominkuang
    except:
        dict_group_3["药物过敏史"] = "获取失败"

    baolushi = tree.xpath('//*[@id="ehrForm"]/div[27]//input[@checked="checked"]')
    baolushi_big = []
    try:
        for i in baolushi:
            baolushi_xuanxiang = i.xpath('following-sibling::span/label/text()')[0]
            baolushi_big.append(baolushi_xuanxiang)
        dict_group_3["暴露史"] = "|".join(baolushi_big)
    except:
        dict_group_3["暴露史"] = None

    try:
        yichuanbingshi = tree.xpath('//*[@id="ehrForm"]/div[29]//input[@checked="checked"]')[0]
        yichuanbingshi_youwu = yichuanbingshi.xpath('following-sibling::span/label/text()')[0]
        yichuanbingshi_big = []
        if yichuanbingshi_youwu == "无":
            dict_group_3["遗传病史"] = "无"
        else:
            yichuanbingshi_big.append(yichuanbingshi_youwu)
            yichuanbingshikuang = tree.xpath('//*[@id="geneticDescription"]/@value')[0]
            yichuanbingshi_big.append(yichuanbingshikuang)
            dict_group_3["遗传病史"] = "|".join(yichuanbingshi_big)
    except:
        dict_group_3["遗传病史"] = None

    try:
        canjiqingkuang = tree.xpath('//*[@id="ehrForm"]/div[30]//input[@name="EHRDETAILS.disability"]')
        list_canjiqingkuang_big = []
        for i in canjiqingkuang:
            if i.get('checked'):
                list_canjiqingkuang_big.append(i.xpath('following-sibling::span/label/text()')[0])
        if list_canjiqingkuang_big == ['无残疾']:
            dict_group_3["残疾情况"] = "无残疾"
        else:
            dict_group_3["残疾情况"] = "|".join(list_canjiqingkuang_big)
    except:
        dict_group_3["残疾情况"] = None

    canjiqingkuang_qita = tree.xpath('//*[@id="disability9"]/@value')[0]

    if canjiqingkuang_qita:
        dict_group_3["其他残疾"] = canjiqingkuang_qita
    else:
        dict_group_3["其他残疾"] = None

    script_content_father = tree.xpath('//script[contains(text(), "var fhFather")]/text()')
    script_content_mother = tree.xpath('//script[contains(text(), "var fhMother")]/text()')
    script_content_brother = tree.xpath('//script[contains(text(), "var fhBrother")]/text()')
    script_content_children = tree.xpath('//script[contains(text(), "var fhChildren")]/text()')

    list_jibing = ["无", "高血压", "糖尿病", "冠心病", "慢性阻塞性肺疾病", "恶性肿瘤", "脑卒中", "重性精神病", "结核病", "肝炎", "先天畸形", "其他"]
    list_father = []
    if script_content_father:
        # 提取并清洗出目标行
        for line in script_content_father[0].split('\n'):
            if 'var fhFather' in line:
                num_list_father = re.findall(r'\d+', line)
                for i in num_list_father:
                    list_father.append(list_jibing[int(i) - 1])
    dict_group_3["父亲"] = "|".join(list_father)


    list_mother = []
    if script_content_mother:
        # 提取并清洗出目标行
        for line in script_content_mother[0].split('\n'):
            if 'var fhMother' in line:
                num_list_mother = re.findall(r'\d+', line)
                for i in num_list_mother:
                    list_mother.append(list_jibing[int(i) - 1])
    dict_group_3["母亲"] = "|".join(list_mother)

    list_brother = []
    if script_content_brother:
        # 提取并清洗出目标行
        for line in script_content_brother[0].split('\n'):
            if 'var fhBrother' in line:
                num_list_brother = re.findall(r'\d+', line)
                for i in num_list_brother:
                    list_brother.append(list_jibing[int(i) - 1])
    dict_group_3["兄弟姐妹"] = "|".join(list_brother)

    list_chileren = []
    if script_content_children:
        # 提取并清洗出目标行
        for line in script_content_children[0].split('\n'):
            if 'var fhChildren' in line:
                num_list_children = re.findall(r'\d+', line)

                for i in num_list_children:  # 1
                    list_chileren.append(list_jibing[int(i) - 1])
    dict_group_3["子女"] = "|".join(list_chileren)
    print("问题不在3")
    return dict_group_3